package top.infopub.mgr.mkt.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.mkt.dao.ProPlotDao;
import top.infopub.mgr.mkt.domain.ProPlotDomain;
import top.infopub.model.mkt.ProPlot;
import top.infopub.mgr.mkt.service.ProPlotService;
import top.infopub.mgr.mkt.vo.ProPlotVo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

@Service
public class ProPlotServiceImpl extends GenericServiceImpl<ProPlot, String> implements
		ProPlotService {

	@Autowired
	private ProPlotDao proPlotDao;

	@Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

	@Autowired
    private WsFlowProcessService wsFlowProcess;

	@Override
	public GenericDao<ProPlot, String> getDao() {
		return proPlotDao;
	}

    @Override
    public Page<ProPlotDomain> selectByDomainWhere(Page<ProPlotDomain> page, ProPlotVo proPlotVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProPlotDomain>)proPlotDao.selectByDomainWhere(proPlotVo);
    }

    @Override
    public Page<ProPlotDomain> selectMyCheckByDomainWhere(Page<ProPlotDomain> page, ProPlotVo proPlotVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProPlotDomain>)proPlotDao.selectMyCheckByDomainWhere(proPlotVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveProPLot(ProPlot proPlot) {
        //执行新增
        if(StringUtils.isEmpty(proPlot.getId())){
            String codes = "mkt_pro_plot";
            String generatorCode = codeGeneratorConfigService.generatorCode(codes);
            proPlot.setCodes(generatorCode);
            AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
            String id = IdFactory.getUUIDUpper();
            String processId = IdFactory.getUUIDUpper();
            proPlot.setId(id);
            proPlot.setAddBy(vo.getId());
            proPlot.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
            // 处理processId
            proPlot.setProcessId(processId);
            proPlot.setProcessStatus("1");//审批中
            proPlot.setUptBy(vo.getId());
            proPlot.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
            proPlot.setTypes("1");
            proPlotDao.insertById(proPlot);

            // TODO 注意 这里根据不同的业务分支判断条件 获得不同enum
            FlowTemplateEnum templateEnum = FlowTemplateEnum.MKT_PRO_PLOT;
            // 处理工作流数据
            wsFlowProcess.launch(templateEnum, id, processId, null);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editProPLot(ProPlot proPlot,String submitType) {
        //执行编辑
        if(StringUtils.isNotEmpty(proPlot.getId())){
            AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
            proPlot.setUptBy(vo.getId());
            proPlot.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
            proPlotDao.updateByIdSelective(proPlot);

            if("2".equalsIgnoreCase(submitType)){
                ProPlot plot=new ProPlot();
                plot.setId(proPlot.getId());
                // 处理processId
                String processId = IdFactory.getUUIDUpper();
                plot.setProcessId(processId);
                plot.setProcessStatus("1");//审批中
                proPlotDao.updateByIdSelective(plot);
                // TODO 注意 这里根据不同的业务分支判断条件 获得不同enum
                FlowTemplateEnum templateEnum = FlowTemplateEnum.MKT_PRO_PLOT;
                // 处理工作流数据
                wsFlowProcess.launch(templateEnum, proPlot.getId(), processId, null);

            }

        }

    }

    @Override
    public ProPlotDomain queryById(String id) {
        return proPlotDao.selectById(id);
    }

}
